$(function () {
    $("#pro_datagrid").datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#pro_toolbar',
        url: '/car/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '车主姓名', field: 'name', align: 'center', width: '100'},
            {
                title: '车辆品牌',
                field: 'carbrand',
                align: 'center',
                width: '150',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {title: '车主型号', field: 'model', align: 'center', width: '100'},
            {title: '车牌号码', field: 'number', align: 'center', width: '100'},
            {title: '排气量/L', field: 'displacement', align: 'center', width: '100'},
            {title: '购买日期', field: 'date', align: 'center', width: '100'},
            {title: '估价', field: 'price', align: 'center', width: '100'},
            {
                title: '功能分类',
                field: 'classification',
                align: 'center',
                width: '100',
                formatter: function (value, row, index) {
                    return value ? "<font color='green'>载客</font>" : "<font color='red'>载物</font>"
                }
            },
            {
                title: '车载大小', field: 'size' ,align:'center', width: '100', formatter: function (value, row, index) {
                    return value ? "<font color='green'>小型</font>" : "<font color='red'>大型</font>"
                }
            },
            {title: '备注', field: 'note', align: 'center', width:'300',height:'300'}
        ]]

    })

    $("#pro_dialog").dialog({
        width: 350,
        height: 470,
        buttons: '#pro_buttons',
        closed: true,
        onClose: function () {
            $("#pro_form").form("clear")
        }
    })


    var methodObj = {
        //添加按钮
        add: function () {
            $("#pro_dialog").dialog("open");
            $("#pro_dialog").dialog('setTitle', '新增产品信息');
        },
        //编辑按钮
        edit: function () {
            var data = $("#pro_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            //数据回显
            if (data.carbrand) {
                console.log(data.carbrand);
                data['carbrand.id'] = data.carbrand.id;
            }
            $("#pro_form").form('load', data);


            $("#pro_dialog").dialog("open");
            $("#pro_dialog").dialog('setTitle', '编辑产品信息');
        },
        //刷新按钮
        reload: function () {
            $("#pro_datagrid").datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            $("#pro_dialog").dialog("close");
            $("#pro_changePwd").dialog("close");
        },
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var number = $("#number").textbox("getValue");

            $("#pro_datagrid").datagrid('load', {
                keyword: keyword,
                number: number
            });
        },
        //添加保存按钮
        save: function (val) {
            var url = $(val).data("url");
            var from = $(val).data("from");

            $("#" + from).form('submit', {
                url: '/car/' + url + '.do',
                success: function (data) {
                    console.log(data)
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            $("#pro_datagrid").datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

})

